package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.CuratorsProfilePageJoinModal;
import com.opensky.qatest.ui.component.CuratorsProfilePageJoinNag;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * 
 * OpenSky - "Curator's profile page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class CuratorsProfilePage extends LoadableComponent<CuratorsProfilePage> implements IPage {

    // ************************ Locators ************************

    private static final String LOGOUT_URL = Properties.get("env.root.url") + "logout";

    private static final String HELP_LINK = "//div[@id='sky-bar']//a[@href='/content/faq']";

    private static final String LOGIN_LINK = "//div[@id='sky-bar']//div[@class='login']/a";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static final String FOLLOW_BTN = "//div[@class='content-header']//div[@class='actions']/a";

    private static final String OFFER_LINK = "//section[@class='curator-collection']//ul/li[1]//a";

    private static final String PRICE = "//section[@class='curator-collection']//ul/li[1]//div[@class='price']//span[@class='dollars']";

    private static final String FOLLOWING_MESSAGE_CONTAINER = "//form[@class='following']/p";

    private static final String FOLLOWING_MESSAGE = "You're following";

    private static final String UNFOLLOW_BTN = "//form[@class='following']/input[@value='Unfollow']";

    private String curatorProfilePageURL = Properties.get("env.root.url") + "%s";

    // ************************ Complex page objects **************************

    private CuratorsProfilePageJoinModal joinModal;

    private CuratorsProfilePageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public CuratorsProfilePage(WebDriverWrapper driver, String curator) {
	this.driver = driver;
	joinModal = new CuratorsProfilePageJoinModal(driver);
	joinNag = new CuratorsProfilePageJoinNag(driver);
	curatorProfilePageURL = String.format(curatorProfilePageURL, curator);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(curatorProfilePageURL);
    }

    public void clickFollow() {
	driver.waitAndClick("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public OfferPage clickOfferLink() {

	String href = driver.waitAssertAndGetElement("OFFER_LINK", By.xpath(OFFER_LINK)).getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	OfferPage offerPage = new OfferPage(driver, curator, product);

	driver.waitAndClick("OFFER_LINK", By.xpath(OFFER_LINK));

	return offerPage.isPageLoaded() ? offerPage : null;
    }

    public LoginPage logOut() {
	LoginPage loginPage = new LoginPage(driver);
	driver.get(LOGOUT_URL);
	return loginPage.isPageLoaded() ? loginPage : null;
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(curatorProfilePageURL, driver.getCurrentUrl());
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(curatorProfilePageURL);
    }

    public boolean isBaseNavElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK)),
		driver.waitAndVerifyElementPresent("HELP_LINK", By.xpath(HELP_LINK)),
		driver.waitAndVerifyElementPresent("TOP_NAV", By.xpath(TOP_NAV)),
		driver.waitAndVerifyElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV)));
    }

    public boolean isPriceShown() {
	return driver.waitAndVerifyElementPresent("PRICE", By.xpath(PRICE));
    }

    public boolean isPriceNotShown() {
	return driver.waitAndVerifyElementNotPresent("PRICE", By.xpath(PRICE));
    }

    public boolean isFollowBtnPresent() {
	return driver.waitAndVerifyElementPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public boolean isFollowBtnNotPresent() {
	return driver.waitAndAssertElementNotPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public boolean isAfterFollowBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementWithTextPresent("FOLLOWING_MESSAGE_CONTAINER",
		By.xpath(FOLLOWING_MESSAGE_CONTAINER), FOLLOWING_MESSAGE), driver.waitAndVerifyElementPresent("UNFOLLOW_BTN",
		By.xpath(UNFOLLOW_BTN)));
    }

    // ************************ GET/SET methods **************************

    public CuratorsProfilePageJoinModal getJoinModal() {
	return joinModal.isComponentLoaded() ? joinModal : null;
    }

    public CuratorsProfilePageJoinNag getJoinNag() {
	return joinNag.isComponentLoaded() ? joinNag : null;
    }

    public String getCuratorURL() {
	return curatorProfilePageURL;
    }

    @Override
    public String getName() {
	return "Curator's profile page";
    }
}
